package com.jellyfishumbrella.GL;

import java.util.Iterator;
import java.util.concurrent.LinkedBlockingQueue;
import com.jellyfishumbrella.GL.AbstractGameLogic;

public class Animator {
	public static LinkedBlockingQueue<AnimObject>	animatables;
	public AnimateThread							animateThread;
	public AbstractGameLogic logic;

	public Animator(AbstractGameLogic logic) {
		this.logic = logic;
		animatables = new LinkedBlockingQueue<AnimObject>();
		animateThread = new AnimateThread("animateThread");
		logic.allThreads.add(animateThread);
		animateThread.start();
	}

	public int add(AnimObject newObj) {
		animatables.add(newObj);
		return animatables.size();
	}

	public void remove(AnimObject removeObj) {
		animatables.remove(removeObj);
	}

	public void clear() {
		animatables.clear();
	}

	public void animateAll(long currTime) {
		animateThread.delta = currTime;
		animateThread.doProcess = true;
	}

	public void animate(AnimObject currObj, long currTime) {
		currObj.isAnimating = true;
		currObj.animate(currTime);
	}

	public class AnimateThread extends ThreadLoop {
		public long		delta	= 0;
		
		public AnimateThread(String name){
			super(name);
		}

		public void process() {
			Iterator<AnimObject> itr = animatables.iterator();
			while (itr.hasNext()) {
				animate(itr.next(), delta);
			}
		}
	}

}
